using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class NPC_DownRoom : NPCBase
{
    [SerializeField] private DialogueRegionDataSO _dialogueRegionDataSO;
    [SerializeField] private DialogueRegionDataSO _dialogueSuccess;
    [SerializeField] private Transform _triggerSuccessTip;
    [SerializeField] private GameObject[] gameObjectPrefab;
    [SerializeField] private Transform[] spawnTrans;
    private List<RiddleProgress> _riddleProgressList = new();
    private bool _isTriggerSuccess = false;
    protected override void Start()
    {
        base.Start();
        _riddleProgressList.Add(RiddleProgress.NoStart);
    }
    private void OnEnable()
    {
        EventCenter.AddEventListener<RiddleProgress>(EventName.PlayerDownRegionProgress, RecordRiddleProgress);
    }

    private void OnDisable()
    {
        EventCenter.RemoveEventListener<RiddleProgress>(EventName.PlayerDownRegionProgress, RecordRiddleProgress);
    }
    protected override void Update()
    {
        if (_isTriggerSuccess == false)
        {
            base.Update();
        }
        else
        {
            _interactToolTip.gameObject.SetActive(false);
            _triggerSuccessTip.gameObject.SetActive(true);

            float floatOffset = Mathf.Sin(Time.time * _floatSpeed) * _floatRange;

            _triggerSuccessTip.position = _originalTipPos + new Vector2(0f, floatOffset);
        }
    }
    
    public override void Interact()
    {
        base.Interact();
        DialogueRegionDataSO dialogueRegionDataSO = _isTriggerSuccess ? _dialogueSuccess : _dialogueRegionDataSO;
        if (_isTriggerSuccess)
        {
            EventCenter.TriggerEvent<DialogueDataBase, Action>(EventName.UI_OpenDialoguePanel, dialogueRegionDataSO, () =>
            {
                for (int i = 0; i < gameObjectPrefab.Length; i++)
                {
                    Instantiate(gameObjectPrefab[i], spawnTrans[i].position, Quaternion.identity);
                }
                GetComponent<Collider2D>().enabled = false;
                _triggerSuccessTip.gameObject.SetActive(false);
            });
            
        }
        else
        {
            EventCenter.TriggerEvent<DialogueDataBase, Action>(EventName.UI_OpenDialoguePanel, dialogueRegionDataSO, () => { });
        }
    }

    /// <summary>
    /// 记录解密进度，在完成各种阶段的任务后触发记录
    /// </summary>
    /// <param name="playerProgress"></param>
    private void RecordRiddleProgress(RiddleProgress riddleProgress)
    {
        if (_riddleProgressList.Contains(riddleProgress) == false)
            _riddleProgressList.Add(riddleProgress);
    }

    public void SetTriggerSuccess()
    {
        _isTriggerSuccess = true;
    }
}
